/**
 * Promisify工具类
 * 将微信小程序API从回调形式转换为Promise形式
 */

/**
 * 将微信API转换为Promise形式
 * @param {Function} fn 需要转换的API函数
 * @returns {Function} 转换后的Promise形式函数
 */
function promisify(fn) {
  return function(obj = {}) {
    return new Promise((resolve, reject) => {
      // 成功回调
      obj.success = function(res) {
        resolve(res);
      };
      
      // 失败回调
      obj.fail = function(err) {
        reject(err);
      };
      
      // 调用原始API
      fn(obj);
    });
  };
}

/**
 * 批量将API转换为Promise形式
 * @param {Object} apis 需要转换的API对象集合
 * @returns {Object} 转换后的API对象集合
 */
function promisifyAll(apis) {
  const result = {};
  
  for (const key in apis) {
    if (typeof apis[key] === 'function') {
      result[key] = promisify(apis[key]);
    } else {
      result[key] = apis[key];
    }
  }
  
  return result;
}

// 常用微信API的Promise版本
const wxPromise = {
  request: promisify(wx.request),
  uploadFile: promisify(wx.uploadFile),
  downloadFile: promisify(wx.downloadFile),
  setStorage: promisify(wx.setStorage),
  getStorage: promisify(wx.getStorage),
  removeStorage: promisify(wx.removeStorage),
  clearStorage: promisify(wx.clearStorage),
  getLocation: promisify(wx.getLocation),
  chooseImage: promisify(wx.chooseImage),
  previewImage: promisify(wx.previewImage),
  getImageInfo: promisify(wx.getImageInfo),
  saveImageToPhotosAlbum: promisify(wx.saveImageToPhotosAlbum),
  startRecord: promisify(wx.startRecord),
  playVoice: promisify(wx.playVoice),
  getBackgroundAudioPlayerState: promisify(wx.getBackgroundAudioPlayerState),
  playBackgroundAudio: promisify(wx.playBackgroundAudio),
  seekBackgroundAudio: promisify(wx.seekBackgroundAudio),
  chooseVideo: promisify(wx.chooseVideo),
  saveVideoToPhotosAlbum: promisify(wx.saveVideoToPhotosAlbum),
  loadFontFace: promisify(wx.loadFontFace),
  connectSocket: promisify(wx.connectSocket),
  chooseAddress: promisify(wx.chooseAddress),
  chooseInvoiceTitle: promisify(wx.chooseInvoiceTitle),
  chooseInvoice: promisify(wx.chooseInvoice),
  scanCode: promisify(wx.scanCode),
  openBluetoothAdapter: promisify(wx.openBluetoothAdapter),
  getBluetoothAdapterState: promisify(wx.getBluetoothAdapterState),
  startBluetoothDevicesDiscovery: promisify(wx.startBluetoothDevicesDiscovery),
  getConnectedBluetoothDevices: promisify(wx.getConnectedBluetoothDevices),
  createBLEConnection: promisify(wx.createBLEConnection),
  closeBLEConnection: promisify(wx.closeBLEConnection),
  getBLEDeviceServices: promisify(wx.getBLEDeviceServices),
  makePhoneCall: promisify(wx.makePhoneCall),
  stopAccelerometer: promisify(wx.stopAccelerometer),
  startAccelerometer: promisify(wx.startAccelerometer),
  stopCompass: promisify(wx.stopCompass),
  startCompass: promisify(wx.startCompass),
  setClipboardData: promisify(wx.setClipboardData),
  getClipboardData: promisify(wx.getClipboardData),
  showToast: promisify(wx.showToast),
  showLoading: promisify(wx.showLoading),
  showModal: promisify(wx.showModal),
  showActionSheet: promisify(wx.showActionSheet),
  setNavigationBarTitle: promisify(wx.setNavigationBarTitle),
  setNavigationBarColor: promisify(wx.setNavigationBarColor),
  saveFile: promisify(wx.saveFile),
  openDocument: promisify(wx.openDocument),
  getSavedFileList: promisify(wx.getSavedFileList),
  getSavedFileInfo: promisify(wx.getSavedFileInfo),
  removeSavedFile: promisify(wx.removeSavedFile),
  getFileInfo: promisify(wx.getFileInfo),
  requestPayment: promisify(wx.requestPayment),
  login: promisify(wx.login),
  checkSession: promisify(wx.checkSession),
  authorize: promisify(wx.authorize),
  getUserInfo: promisify(wx.getUserInfo),
  navigateTo: promisify(wx.navigateTo),
  redirectTo: promisify(wx.redirectTo),
  navigateBack: promisify(wx.navigateBack),
  switchTab: promisify(wx.switchTab),
  reLaunch: promisify(wx.reLaunch),
  getSystemInfo: promisify(wx.getSystemInfo),
  getNetworkType: promisify(wx.getNetworkType),
  setScreenBrightness: promisify(wx.setScreenBrightness),
  getScreenBrightness: promisify(wx.getScreenBrightness),
  setKeepScreenOn: promisify(wx.setKeepScreenOn),
  vibrateLong: promisify(wx.vibrateLong),
  vibrateShort: promisify(wx.vibrateShort),
  addPhoneContact: promisify(wx.addPhoneContact),
  getHCEState: promisify(wx.getHCEState),
  startHCE: promisify(wx.startHCE),
  stopHCE: promisify(wx.stopHCE),
  sendHCEMessage: promisify(wx.sendHCEMessage),
  startWifi: promisify(wx.startWifi),
  stopWifi: promisify(wx.stopWifi),
  connectWifi: promisify(wx.connectWifi),
  getWifiList: promisify(wx.getWifiList),
  setWifiList: promisify(wx.setWifiList),
  getConnectedWifi: promisify(wx.getConnectedWifi)
};

module.exports = promisify;
module.exports.promisifyAll = promisifyAll;
module.exports.wxPromise = wxPromise; 